package org.yx.service.sbas.view_product;

import com.xiaoleilu.hutool.util.StrUtil;
import org.dbist.dml.Page;
import org.dbist.dml.Query;
import org.yx.models.MbasCdsDtl;
import org.yx.models.SbasProdMat;
import org.yx.utils.BaseService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询产品信息
 * @author zxw
 * @since 2018/03/25
**/
public class SbasViewProductService extends BaseService {
	public SbasViewProductOut viewProduct(SbasViewProductIn in) throws Exception {Thread.sleep(300);
		Query query = new Query(in.getPageIndex(), in.getPageSize());
		query.addFilter("orgCode", in.getUserToken().getOrgCode());
		if (!StrUtil.isBlank(in.getMatCode())) {
			query.addFilter("matCode", in.getMatCode());
		}
		query.addFilter("matStatus", "<>", 'D');

		Page<SbasProdMat> prodPage = selectPage(SbasProdMat.class, query);
		List<Product> prodList = new ArrayList<Product>();
		if (prodPage.getTotalSize() > 0) {
			query.removeFilter("matCode");
			query.removeFilter("matStatus");
			query.addFilter("groupName", "MATERIAL_TYPE");
			List<MbasCdsDtl> dtlList = selectList(MbasCdsDtl.class, query);
			Map<String, String> typeMap = new HashMap<String, String>();
			for (MbasCdsDtl dtl : dtlList) {
				typeMap.put(dtl.getKey1(), dtl.getData1());
			}

			for (SbasProdMat mat : prodPage.getList()) {
				Product prod = new Product();
				copyProperties(mat, prod);
				prod.setMatTypeDesc(typeMap.get(prod.getMatType()));
				prodList.add(prod);
			}
		}

		SbasViewProductOut out = new SbasViewProductOut();
		out.setProductList(prodList);
		out.setTotalSize(prodPage.getTotalSize());

		return out;
	}
}